package qst.com.bacK.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import qst.com.bacK.utils.RequestUtil;
import qst.com.bacK.utils.WebUtil;
import qst.com.bacK.exception.ExamException;
import qst.com.bacK.pojo.entity.AssessmentType;
import qst.com.bacK.service.AssessmentService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Author:   msh
 * Date:     2024/10/11 下午2:38
 * Description:
 */

@Controller
@RequestMapping("assessment")
public class AssessmentController {
    @Autowired
    private AssessmentService assessmentService;

    @RequestMapping("/create.action")
    public void toCreateAssessment(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        WebUtil.forward(req, resp, "/assessment/create.jsp");
    }

    // 删除
    @RequestMapping("/delete.action")
    public void deleteAssessment(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        int id = RequestUtil.getInt(req, "id");

        try {
            assessmentService.deleteAssessment(id);
            req.getSession().setAttribute("_message_key", "科目已删除");
        } catch (ExamException ex) {
            req.getSession().setAttribute("_error_key", ex.getMessage());
        }
        resp.sendRedirect("/assessment/list.action");
    }

    // 编辑
    @RequestMapping("/edit.action")
    public void editAssessment(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        int id = RequestUtil.getInt(req, "id");
        AssessmentType assessment = assessmentService.findAssessmentById(id);
        req.setAttribute("assessment", assessment);
        WebUtil.forward(req, resp, "/assessment/edit.jsp");
    }

    // 列表
    @RequestMapping("/list.action")
    public void listAssessment(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        List<AssessmentType> assessmentList = assessmentService.findAllAssessment();
        req.setAttribute("assessmentList", assessmentList);
        WebUtil.forward(req, resp, "/assessment/list.jsp");
    }

    // 保存
    @RequestMapping("/save.action")
    public void saveAssessment(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        AssessmentType sj = new AssessmentType();
        sj.setTitle(RequestUtil.getString(req, "title"));
        sj.setCost(RequestUtil.getDouble(req, "cost"));
        sj.setStatus(RequestUtil.getInt(req, "status"));
        try {
            assessmentService.saveAssessment(sj);
            AssessmentType assessmentType = assessmentService.findByTitle(sj.getTitle());
            req.getSession().setAttribute("_message_key", "科目信息已被保存到数据库");
//            WebUtil.redirect(req, resp, "/assessment/view.action?id=" + sj.getId());
            resp.sendRedirect("/assessment/view.action?id=" + assessmentType.getId());
        } catch (ExamException ex) {
            req.getSession().setAttribute("_error_key", ex.getMessage());
            req.setAttribute("assessment", sj);
            WebUtil.forward(req, resp, "/assessment/create.jsp");
        }
    }

    // 更新
    @RequestMapping("/update.action")
    public void updateAssessment(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        AssessmentType sj = new AssessmentType();
        sj.setId(RequestUtil.getInt(req, "id"));
        sj.setTitle(RequestUtil.getString(req, "title"));
        sj.setCost(RequestUtil.getDouble(req, "cost"));
        sj.setStatus(RequestUtil.getInt(req, "status"));
        try {
            assessmentService.updateAssessment(sj);
            req.getSession().setAttribute("_message_key", "考核类型信息已被保存到数据库");
            resp.sendRedirect("/assessment/view.action?id=" + sj.getId());
        } catch (ExamException ex) {
            req.getSession().setAttribute("_error_key", ex.getMessage());
            req.setAttribute("assessment", sj);
            WebUtil.forward(req, resp, "/assessment/edit.jsp");
        }
    }

    // view
    @RequestMapping("/view.action")
    public void viewAssessment(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        int id = RequestUtil.getInt(req, "id");
        AssessmentType assessment = assessmentService.findAssessmentById(id);
        req.setAttribute("assessment", assessment);
        WebUtil.forward(req, resp, "/assessment/view.jsp");
    }
}
